package com.opcua.client.start;
import com.opcua.client.mvc.main.MainView;
import com.opcua.client.mvc.service.PersistenceService;
import com.opcua.client.mvc.service.OpcUaClientConnector;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import com.airhacks.afterburner.injection.Injector;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
@Slf4j(topic = "启动类")
public class OpcUAClient extends Application {
  private final DoubleProperty sceneWidthProperty = new SimpleDoubleProperty(1024);
  private final DoubleProperty sceneHeightProperty = new SimpleDoubleProperty(800);
  @Override
  public void start(Stage stage) throws Exception {
//    stage.setTitle("OPC-UA client.fx (" + System.getProperty("javafx.runtime.version") + ")");
    stage.setTitle("GY-FMS数控机床OPC-UA-CLIENT软件V1.0");
    stage.setResizable(true);
    Injector.setLogger(log::trace);
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    lc.getLogger("org.eclipse.milo.opcua.sdk.client.subscriptions").setLevel(Level.INFO);
    PersistenceService session = Injector.instantiateModelOrService(PersistenceService.class);
    Injector.instantiateModelOrService(OpcUaClientConnector.class);
    session.bind(sceneWidthProperty, "scene.width");
    session.bind(sceneHeightProperty, "scene.height");
    MainView main = new MainView();
    final Scene scene = new Scene(main.getView(), sceneWidthProperty.get(), sceneHeightProperty.get());
    stage.setOnCloseRequest((e) -> {
      sceneWidthProperty.set(scene.getWidth());
      sceneHeightProperty.set(scene.getHeight());
      Injector.forgetAll();
      System.exit(0);
    });
    stage.setScene(scene);
    stage.getIcons().add(new Image(this.getClass().getClassLoader().getResource("icon.png").toExternalForm()));
    stage.show();
  }
  public static void main(String[] args) {
    launch(args);
  }
}
